package com.yunnuo.system.controller;


import com.yunnuo.common.controller.BaseController;
import com.yunnuo.common.web.ResultData;
import com.yunnuo.common.web.page.TableDataInfo;
import com.yunnuo.system.domain.Role;
import com.yunnuo.system.domain.User;
import com.yunnuo.system.service.IRoleService;
import com.yunnuo.system.service.IUserService;
import net.sf.json.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @api {title} 用户信息comtroller
 * @apiName UserController
 * @apiGroup System-UserController
 * @apiDescription Created by 魏洪福(15064000912@163.com) on  2018/5/25.
 */
@Controller
@RequestMapping("/system/user")
public class UserController  extends BaseController {

    private String prefix = "system/user";

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;


    @RequiresPermissions("system:user:view")
    @GetMapping()
    public String user()
    {
        return prefix + "/user";
    }

    @RequiresPermissions("system:user:list")
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo list(User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("system:user:edit")
    @GetMapping("/edit/{userId}")
    public String edit(@PathVariable("userId") Integer userId, Model model)
    {
        User user = userService.selectUserById(userId);
        List<Role> roles = roleService.selectRolesByUserId(userId);
        model.addAttribute("roles", roles);
        model.addAttribute("user", user);
        return prefix + "/edit";
    }

    /**
     * 新增用户
     */
    @RequiresPermissions("system:user:add")
    @GetMapping("/add")
    public String add(Model model)
    {
        List<Role> roles = roleService.selectRoleAll();
        model.addAttribute("roles", roles);
        return prefix + "/add";
    }

    @RequiresPermissions("system:user:resetPwd")
    @GetMapping("/resetPwd/{userId}")
    public String resetPwd(@PathVariable("userId") Integer userId, Model model)
    {
        User user = userService.selectUserById(userId);
        model.addAttribute("user", user);
        return prefix + "/resetPwd";
    }

    @RequiresPermissions("system:user:resetPwd")
    @PostMapping("/resetPwd")
    @ResponseBody
    public JSON resetPwd(User user)
    {
        ResultData data = new ResultData();
        int rows = userService.resetUserPwd(user);
        if (rows > 0)
        {
            data.setStat(ResultData.STAT_SUCCESS);
            data.setData(rows);
            data.setMessage("操作成功！");
        }else{
            data.setStat(ResultData.STAT_FAILURE);
            data.setData("");
            data.setMessage("操作失败！");
        }
        return data.toJson();
    }

    @RequiresPermissions("system:user:remove")
    @RequestMapping("/remove/{userId}")
    @ResponseBody
    public JSON remove(@PathVariable("userId") Integer userId)
    {
        ResultData data = new ResultData();
        User user = userService.selectUserById(userId);
        if (user == null)
        {
            data.setStat(ResultData.STAT_FAILURE);
            data.setData("");
            data.setMessage("用户不存在！");
        }

        user.setUserStatus("2");
        int num = userService.updateUser(user);
        if(num>0){
            data.setStat(ResultData.STAT_SUCCESS);
            data.setData(num);
            data.setMessage("操作成功！");
        }else{
            data.setStat(ResultData.STAT_FAILURE);
            data.setData("");
            data.setMessage("删除失败！");
        }
        return data.toJson();
    }

    @RequiresPermissions("system:user:batchRemove")
    @PostMapping("/batchRemove")
    @ResponseBody
    public JSON batchRemove(@RequestParam("ids[]") String[] ids)
    {
        ResultData data = new ResultData();
        int rows = userService.batchDeleteUser(ids);
        if (rows > 0)
        {
            data.setStat(ResultData.STAT_SUCCESS);
            data.setData(rows);
            data.setMessage("操作成功！");
        }else{
            data.setStat(ResultData.STAT_FAILURE);
            data.setData("");
            data.setMessage("操作失败！");
        }
        return data.toJson();
    }

    /**
     * 保存用户
     */
    @RequiresPermissions("system:user:save")
    @PostMapping("/save")
    @ResponseBody
    public JSON save(User user)
    {
        ResultData data = new ResultData();
        int rows = userService.saveUser(user);
        if (rows > 0)
        {
            data.setStat(ResultData.STAT_SUCCESS);
            data.setData(rows);
            data.setMessage("操作成功！");
        }else{
            data.setStat(ResultData.STAT_FAILURE);
            data.setData("");
            data.setMessage("操作成功！");
        }
      return data.toJson();
    }

    /**
     * 校验用户名
     */
    @PostMapping("/checkLoginNameUnique")
    @ResponseBody
    public String checkLoginNameUnique(User user)
    {
        String uniqueFlag = "0";
        if (user != null)
        {
            uniqueFlag = userService.checkLoginNameUnique(user.getLoginName());
        }
        return uniqueFlag;
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkPhoneUnique")
    @ResponseBody
    public String checkPhoneUnique(User user)
    {
        String uniqueFlag = "0";
        if (user != null)
        {
            uniqueFlag = userService.checkPhoneUnique(user);
        }
        return uniqueFlag;
    }

    /**
     * 校验手机号码
     */
    @PostMapping("/checkEmailUnique")
    @ResponseBody
    public String checkEmailUnique(User user)
    {
        String uniqueFlag = "0";
        if (user != null)
        {
            uniqueFlag = userService.checkEmailUnique(user);
        }
        return uniqueFlag;
    }
}